package cn.gcyl.module.biz.service.aboutplatform;

import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import cn.gcyl.module.biz.controller.admin.aboutplatform.vo.*;
import cn.gcyl.module.biz.dal.dataobject.aboutplatform.AboutPlatformDO;
import cn.gcyl.module.biz.dal.mysql.aboutplatform.AboutPlatformMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.gcyl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gcyl.module.system.enums.ErrorCodeConstants.ABOUT_PLATFORM_NOT_EXISTS;

/**
 * 平台简介 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class AboutPlatformServiceImpl implements AboutPlatformService {

    @Resource
    private AboutPlatformMapper aboutPlatformMapper;

    @Override
    public Long createAboutPlatform(AboutPlatformSaveReqVO createReqVO) {
        // 插入
        AboutPlatformDO aboutPlatform = BeanUtils.toBean(createReqVO, AboutPlatformDO.class);
        aboutPlatformMapper.insert(aboutPlatform);

        // 返回
        return aboutPlatform.getId();
    }

    @Override
    public void updateAboutPlatform(AboutPlatformSaveReqVO updateReqVO) {
        // 校验存在
        validateAboutPlatformExists(updateReqVO.getId());
        // 更新
        AboutPlatformDO updateObj = BeanUtils.toBean(updateReqVO, AboutPlatformDO.class);
        aboutPlatformMapper.updateById(updateObj);
    }

    @Override
    public void deleteAboutPlatform(Long id) {
        // 校验存在
        validateAboutPlatformExists(id);
        // 删除
        aboutPlatformMapper.deleteById(id);
    }

    @Override
        public void deleteAboutPlatformListByIds(List<Long> ids) {
        // 删除
        aboutPlatformMapper.deleteByIds(ids);
        }


    private void validateAboutPlatformExists(Long id) {
        if (aboutPlatformMapper.selectById(id) == null) {
            throw exception(ABOUT_PLATFORM_NOT_EXISTS);
        }
    }

    @Override
    public AboutPlatformDO getAboutPlatform(Long id) {
        return aboutPlatformMapper.selectById(id);
    }

    @Override
    public PageResult<AboutPlatformDO> getAboutPlatformPage(AboutPlatformPageReqVO pageReqVO) {
        return aboutPlatformMapper.selectPage(pageReqVO);
    }

}